# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import json
from scrapy.utils.project import get_project_settings
import os
from scrapy.pipelines.images import ImagesPipeline
import scrapy
from scrapy.log import logger
import pymongo
from scrapy.conf import settings



class SpcrawlPipeline(object):
    def process_item(self, item, spider):
        return item


class FrxxzPipeline(object):

    def __init__(self):
        self.filename = codecs.open("frxxz.json", "w", encoding="utf-8")


    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\r\n"
        self.filename.write(content)
        return item

    def spider_closed(self, spider):
        self.filename.close()

class TencentPipeline(object):

    def __init__(self):
        self.filename = codecs.open("tencent.json", "w", encoding="utf-8")


    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\r\n"
        self.filename.write(content)
        return item

    def spider_closed(self, spider):
        self.filename.close()


class ImagesPipeLines(ImagesPipeline):

    IMAGES_STORE = get_project_settings().get("IMAGES_STORE")

    def get_media_requests(self, item, info):
        image_url = item["imagelink"]
        yield scrapy.Request(image_url)

    def item_completed(self, result, item, info):
        image_path = [x["path"] for ok, x in result if ok]
        logger.info("pipeline 的 image_path : %s"%image_path)
        # os.rename(self.IMAGES_STORE+"/"+image_path[0], self.IMAGES_STORE+"/"+item["nickname"]+".jpg")
        # item["imagePath"] = self.IMAGES_STORE + "/" + item["nickname"]
        return item


class DongguanPipeline(object):

    def __init__(self):
        self.filename = codecs.open("sunwz.json", "w", encoding="utf-8")


    def process_item(self, item, spider):
        content = json.dumps(dict(item), ensure_ascii=False) + ",\r\n"
        self.filename.write(content)
        return item

    def spider_closed(self, spider):
        self.filename.close()


class DoubanPipeline(object):

    def __init__(self):
        # mongo
        host = settings["MONGODB_HOST"]
        port = settings["MONGODB_PORT"]
        dbname = settings["MONGODB_DBNAME"]
        # 链接
        client = pymongo.MongoClient(host=host, port=port)

        # 数据库
        mdb = client[dbname]

        #表名
        self.post = mdb[settings["MONGODB_DOCNAME"]]

        # self.filename = codecs.open("douban.json", "w", encoding="utf-8")


    def process_item(self, item, spider):
        # content = json.dumps(dict(item), ensure_ascii=False) + ",\r\n"
        # self.filename.write(content)
        # return item
        data = dict(item)
        self.post.insert(data)
        return item

    def spider_closed(self, spider):
        # self.filename.close()
        pass